var sacadosDeTabla = new Array();

function Graficando()
{
    var divTrans = document.createElement("div");
    divTrans.setAttribute("id", "divTrans");
    divTrans.setAttribute("class", "fondoPopUp");

    var login = document.createElement("div");
    login.setAttribute("id", "login");
    login.setAttribute("class", "popupWrapper");

    var divHead = document.createElement("div");
    divHead.setAttribute("class", "divHead");

    var title = document.createElement("div");
    title.setAttribute("class", "divHead1");

    var span = document.createElement("p");
    span.setAttribute("id", "spanp");
    span.innerHTML = "<b>Gráfico Pastel</b>";
    title.appendChild(span);
    divHead.appendChild(title);

    var close = document.createElement("div");
    close.setAttribute("class", "divHead2");
    var imgclose = document.createElement("img");
    imgclose.setAttribute("src", "Images/close.png");
    imgclose.setAttribute("id", "imgClose");
    imgclose.setAttribute("class", "closeWin");
    imgclose.setAttribute("onclick", "CierraVentana()");
    close.appendChild(imgclose);
    divHead.appendChild(close);

    login.appendChild(divHead);

    var line = document.createElement("hr");
    login.appendChild(line);
 
    if(!(document.getElementById("loginDivPrin")))
    {
        var divPrin = document.createElement("div");
        divPrin.setAttribute("id", "loginDivPrin");
    }
    var divBotones= document.createElement("div");
    divBotones.setAttribute("id", "divBotones");
    
   
    login.appendChild(divBotones);    
    login.appendChild(divPrin);
    
    divTrans.appendChild(login);

    document.getElementById("main").appendChild(divTrans);
    document.getElementById("main").appendChild(login);//divPrincipal

    var div = $('#login');
    div.css({
        'left': ($(window).width() / 2 - $(div).width() / 2) + 'px',
        'top': ($(window).height() / 2 - $(div).height() / 4 - 200) + 'px'
    });

    $("#login").fadeIn();
    
}

function CierraVentana()
{
     $("#login").fadeOut();
        window.setTimeout(function() {
            //document.getElementById("main").removeChild(document.getElementById("divTrans"));
            //document.getElementById("main").removeChild(document.getElementById("login"));
            CierraGrafico();
            
        }, 300);
}

function creaComboBox(div)
{
    var combo = document.createElement("select");
    combo.setAttribute("size","1");
    combo.setAttribute("id","comboGrafico");  
    
    
    div.appendChild(combo);
    CreaOption(combo,"hola");
}


function CreaOption(combo, option) { //recibe a cual combo se le agrega y un string option
    var valor = option;
    var option = document.createElement("option");
    option.text = valor;
    option.value = valor;
    combo.add(option);
}

function MuestaGrafico(tbspc)
{
    $('.pestana').hide();   
    $('#divTrans').show();  
    $('#login').show();   
    creaGrafico(tbspc);
}
function CierraGrafico()
{
    $('#login').hide(); 
    $('#divTrans').hide();   
    $('#divPrincipal').show();   
}

function OcultaGrafico()
{
    $('#login').hide();   
    $('#divTrans').hide();   
}
function creaBotones(div)
{
  
    //creaComboBox(div);
    
    var bot2 = document.createElement("input");
    bot2.type = "submit";
    bot2.value = "Aceptar";
    bot2.setAttribute("class", "botL");
    bot2.setAttribute("tabindex", "3");
    bot2.id = "bot2LA";
    bot2.name = "bot2LA";
    div.appendChild(bot2);
} 
google.load("visualization", "1",{packages:["corechart"]});
google.setOnLoadCallback(creaGrafico);


function creaGrafico(tbspc)
{   
    var data = new google.visualization.DataTable();
    data.addColumn('string','Nombre Tabla');
    data.addColumn('number','Tamaños Tabla'); 
    if(tbspc != null && tbspc != "")
    {
       
        var tablas = llena_grafico(tbspc);  
        
        var options =
        {
            title: tbspc, 
            is3D: true
        };  
        if(tablas)
        {
         for(x in tablas)
            data.addRow([tablas[x][0],tablas[x][1]]); 
        }
        else
            data.addRow(["Vacío", 50]); 
    }
    else
    { 
        var options =
        {
            title: "Vacío", 
            is3D: true
        };   
        data.addRow(["Vacío", 50]); 
    }
    if(!(document.getElementById("loginDivPrin")))
    {
        var divprin = document.createElement("div"); 
        divprin.setAttribute("id", "loginDivPrin");
        document.getElementById("login").appendChild(divPrin);
    }
    if(document.getElementById("divchart"))
    { 
        document.getElementById("loginDivPrin").removeChild(document.getElementById("divchart")); 
        
    } 
    var divchart = document.createElement("div"); 
    divchart.setAttribute("id", "divchart");
    document.getElementById("loginDivPrin").appendChild(divchart);
    var chart = new google.visualization.PieChart(document.getElementById("divchart")); 
    chart.draw(data, options);
} 

function quitaRow(row, tbspc, used, pronostico)
{
    //alert(row+" "+tbspc+" "+used+" "+pronostico); 
    var table = document.getElementById("tablaPRC1"); 
    table.deleteRow(row); 
    sacadosDeTabla.push([tbspc,used,pronostico]); 
    var sel = document.getElementById("rowAdder"); 
    var val = document.createElement("option"); 
    val.innerHTML = tbspc; 
    sel.appendChild(val);
} 

function progresando(tbspc, used,pron)
{   
    var comillas = '"';
    var spanVal = Math.round(used);
    var barra = document.createElement("progress"); 
    barra.setAttribute("max","100");
    barra.setAttribute("value",used);  
    barra.setAttribute("class",'pgrs');
    barra.setAttribute("id",'id_'+row+"_"+spanVal+"'");  
    var span = document.createElement("span"); 
    span.setAttribute("class",'span-bar');
    span.setAttribute("id","spn");   
    span.innerHTML = spanVal+"% &nbsp"; 
    var spanPr = document.createElement("span");
    spanPr.setAttribute("class",'pron-span');
    spanPr.innerHTML = pron;
    var table = document.getElementById("tablaPRC1"); 
    var t_size =  $('#tablaPRC1 tr').length; 
    var row = table.insertRow(t_size);
    var cellName = row.insertCell(0);
    cellName.innerHTML = tbspc;
    cellName.setAttribute("onclick","MuestaGrafico('"+tbspc+"')"); 
    var cellNameII = row.insertCell(1);
    var div = document.createElement("div"); 
    div.setAttribute("class","rows"); 
    div.appendChild(barra); 
    div.appendChild(span); 
    div.appendChild(spanPr); 
    var bot = document.createElement("button");  
    bot.setAttribute("class","botPopup btPorgress");
    bot.setAttribute("onclick","quitaRow( "+t_size+", "+comillas+tbspc+comillas+", "+used+", '"+pron+"')"); 
    bot.innerHTML = "<img src='images/close_1.png'></img>"; 
    div.appendChild(bot);   
    cellNameII.appendChild(div); 
} 

function addNuevaRow()
{
    var tbspc = getSelectedText("rowAdder"); 
    if(tbspc != null)
    {
        for(x in sacadosDeTabla)
        { 
            console.log(sacadosDeTabla[x][0]+" "+tbspc);
            if(sacadosDeTabla[x][0] == tbspc)
            { 
                console.log(sacadosDeTabla[x][0]+" T");
                progresando(sacadosDeTabla[x][0],sacadosDeTabla[x][1],sacadosDeTabla[x][2]);  
                var sel = document.getElementById("rowAdder"); 
                sel.remove(sel.selectedIndex);
                return true;
            }
        }  
        return false;
    }
    return false;
}
 
 
 function getSelectedText(elementId) 
 {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1 || elt.selectedIndex == 0)
        return null;

    return elt.options[elt.selectedIndex].text;
}